#!/bin/bash


# TERMINAL COLORS -----------------------------------------------------------------

NONE='\033[00m'
RED='\033[01;31m'
GREEN='\033[01;32m'
YELLOW='\033[01;33m'

###################
# Interfaces      #
###################
EXT_NIC="eth0"
INT_NIC="eth1"

#----------------------------------------------------------------------------------------
total_clients=0
total_active_groups=0

groups_index=()
groups_name=()
groups_id=()
groups_down=()
groups_up=()
groups_aggr=()
groups_prio=()
groups_active=()
groups_client_count=()
groups_sub_count=()



##########################################################################
# load groups from definition file and compute traffic values like ceil  #
##########################################################################
function prepare_group_definitions {
	local z=0
	#check if group.definitions exist, if not, create it with default Fiber30 group
	if [ -s "config/group.definitions" ]; then
		echo -e "${GREEN}found group.definitions and processing...${NONE}"
	else
		echo -e "${RED} group.definitions NOT FOUND!!! recreating...${NONE}"
		mkdir config
		touch config/group.definitions;
		echo "#name           download        upload          aggregation     prio_group" > config/group.definitions
		echo "Fiber30         30720           5120            8               0" >> config/group.definitions
	fi

	#read group.definitions and fill arrays with values
        while read line
        do
                #take line and split it into array, first value is main! (aka group,client..etc)
                arrl=($(echo $line | tr " " "\n"))
		if [ -z "$line" ]; then
			echo "EMPTY LINE"
		else 
			case "${arrl[0]}" in
                '#'*)  
                        ;;
                *)      echo "---------------------------------------------------------------"
			echo "found GROUP called ${arrl[0]} | download $((arrl[1] / 1024)) Mbs | upload $((arrl[2] / 1024)) Mbs | aggr ${arrl[3]}"                        
			groups_name[$z]=${arrl[0]}
			groups_id[$z]=$((${z}+1))
                        groups_down[$z]=${arrl[1]}
                        groups_up[$z]=${arrl[2]}
                        groups_aggr[$z]=${arrl[3]}
                        groups_prio[$z]=${arrl[4]}
			groups_active[$z]=0

			if [ -f ${arrl[0]}.group ];
			then
				groups_active[$z]=1
			fi
			z=$((${z}+1))
                        ;;
                esac
		fi
        done <config/group.definitions

	echo "got ${#groups_id[@]} "
	echo "END OF DEFINITIONS"	
}


#################################################
# get all clients for certain group             #
#################################################
function count_clients {

	local size=${#groups_id[@]}

	for (( i=0; i <size; i++ ))
	do
		if [ -f ${groups_name[$i]}.group ];
		then
		
			#sort and deduplicate
	        	cat ${groups_name[${i}]}.group|sort -n|uniq 1>/dev/null

			while read line
        		do
				total_clients=$((${total_clients}+1))
				groups_client_count[$i]=$((${groups_client_count[$i]}+1))
        		done <${groups_name[${i}]}.group
		else
   			echo -e "${RED}#### ERROR! #### File ${groups_name[${i}]}.group does not exist but is defined in group.definitions${NONE}"
		fi
	done

	# STATS ========================================
	f=0
	for i in ${groups_name[@]};
	do
		groups_sub_count[$i]=$(($((${groups_client_count[$f]}/${groups_aggr[$f]}))+1))
		echo "got $i with ${groups_client_count[$f]} clients divided into ${groups_sub_count[$i]} subs"
		((f++))
	done
}


################################################
# Shaping and tree organization                #
################################################
function tc_root {

	echo "count: ${#group_definitions_name[*]}" 
	for ((f=0; f<${#groups_id[*]}; f++));
	do
		echo "INDEX: $f NAME: ${groups_name[$f]}"
		if [[ ${groups_active[$f]} -eq 1 ]];
		then
			echo ${groups_name[$f]}

			echo "parent 1:1 classid 1:${groups_id[$f]} htb rate ${master_total_rate} ceil ${group_definitions_totalband}Kbit"
		else
			echo ${groups_name[$f]}
			echo "group not active... SKIP"
		fi
	done
}



#tc_init
prepare_group_definitions
count_clients
